home *** CD-ROM | disk | FTP | other *** search
/ Symantec Visual Cafe for Java 2.5 / symantec-visual-cafe-2.5-database-dev-edition.iso / Visual Cafe Pro v1.0 / SOURCE.BIN / SoundPlayer.java < prev    next >
Encoding:
Java Source  |  1997-06-19  |  4.0 KB  |  171 lines

  1. package symantec.itools.multimedia;
  2.  
  3.  
  4. import java.net.URL;
  5. import java.net.URLConnection;
  6. import java.net.MalformedURLException;
  7. import java.io.IOException;
  8. import java.io.InputStream;
  9. import java.util.Enumeration;
  10. import java.util.Vector;
  11. import sun.audio.AudioStream;
  12. import sun.audio.AudioDataStream;
  13. import sun.audio.AudioPlayer;
  14. import sun.audio.AudioData;
  15.  
  16.  
  17. /**
  18.  * SoundPlayer component.
  19.  * @version 1.0, Nov 26, 1996
  20.  * @author Symantec
  21.  */
  22.  
  23.  
  24. public class SoundPlayer
  25. {
  26.     private Vector clips;
  27.     private boolean sync = true;
  28.     private SoundViewerThread spt = null;
  29.     private int repeatCt = 1;
  30.  
  31.     public static final int INFINITE = -1;
  32.  
  33.     public SoundPlayer() {
  34.         clips = new Vector();
  35.     }
  36.  
  37.     public void setSyncMode(boolean f) {
  38.         sync = f;
  39.         if (spt != null)
  40.             spt.doSync(f);
  41.     }
  42.  
  43.     public boolean getSyncMode() {
  44.         return sync;
  45.     }
  46.  
  47.     public void setRepeat(int c) {
  48.         repeatCt = c;
  49.     }
  50.  
  51.     public int getRepeat() {
  52.         return repeatCt;
  53.     }
  54.  
  55.     public void addURL(URL u) {
  56.         InputStream in = null;
  57.         AudioData data;
  58.         try {
  59.             try {
  60.                 URLConnection uc = u.openConnection();
  61.                 uc.setAllowUserInteraction(true);
  62.                 in = uc.getInputStream();
  63.                 AudioStream as = new AudioStream(in);
  64.                 clips.addElement(new SoundViewerItem(as.getData(),
  65.                     (int)(((double)as.getLength() / 7168.0) * 1000.0)));
  66.             } finally {
  67.                 if (in != null)
  68.                     in.close();
  69.             }
  70.         } catch (IOException e) {
  71.         }
  72.     }
  73.  
  74.     public void addStringURL(String url) {
  75.         try {
  76.             addURL(new URL(url));
  77.         } catch (MalformedURLException e) {
  78.         }
  79.     }
  80.  
  81.     public void setURLList(URL[] list) {
  82.         clips.removeAllElements();
  83.         for (int i = 0; i < list.length; ++i)
  84.             addURL(list[i]);
  85.     }
  86.  
  87.     public URL[] getURLList() {
  88.         int len = clips.size();
  89.         URL[] ret = new URL[len];
  90.         for (int i = 0; i < len; ++i)
  91.             ret[i] = (URL)clips.elementAt(i);
  92.         return ret;
  93.     }
  94.  
  95.     public void play() {
  96.         spt = new SoundViewerThread(clips, sync, repeatCt);
  97.         spt.start();
  98.     }
  99.  
  100.     public void stop() {
  101.         if (spt != null)
  102.             spt.doStop();
  103.     }
  104.  
  105.     public void stop(int delay) {
  106.         try {
  107.             Thread.sleep(delay);
  108.         } catch (InterruptedException ie) {
  109.         }
  110.         stop();
  111.     }
  112. }
  113.  
  114. class SoundViewerThread extends Thread
  115. {
  116.     private Vector clips;
  117.     private boolean doEnd = false;
  118.     private AudioDataStream curStream = null;
  119.     private int repeatCt = 0;
  120.     private boolean sync = true;
  121.  
  122.     SoundViewerThread(Vector clips, boolean sync, int rct) {
  123.         this.clips = clips;
  124.         this.sync  = sync;
  125.         repeatCt   = rct;
  126.     }
  127.  
  128.     public void run() {
  129.         while (repeatCt == -1 || repeatCt-- > 0) {
  130.             int nClips = clips.size();
  131.             for (int index = 0; index < nClips && !doEnd; ++index) {
  132.                 SoundViewerItem spi = (SoundViewerItem)clips.elementAt(index);
  133.                 curStream = new AudioDataStream(spi.data);
  134.                 AudioPlayer.player.start(curStream);
  135.                 if (sync) {
  136.                     try {
  137.                         Thread.sleep(spi.delay);
  138.                     } catch (InterruptedException ie) {
  139.                     }
  140.                 }
  141.             }
  142.             curStream = null;
  143.         }
  144.         doEnd = false;
  145.     }
  146.  
  147.     void doSync(boolean f) {
  148.         sync = f;
  149.     }
  150.  
  151.     void doStop() {
  152.         if (curStream != null)
  153.             AudioPlayer.player.stop(curStream);
  154.         doEnd = true;
  155.     }
  156. }
  157.  
  158.  
  159. class SoundViewerItem
  160. {
  161.     AudioData data;
  162.     int delay;
  163.  
  164.     SoundViewerItem(AudioData data, int delay)
  165.     {
  166.         this.data = data;
  167.         this.delay = delay;
  168.     }
  169. }
  170.  
  171.